﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using DashFire;
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;

public class ResBuildProcesser
{
  public static bool BuildAllResources(int buildTarget = -1)
  {
    bool ret = true;
    try {
      LogBuildStatus("ResBuildConfig.Load() Start");
      if (!ResBuildConfig.Load()) {
        ResBuildLog.Warn("BuildAllResources.LoadConfig failed!");
        return false;
      }

      LogBuildStatus("ResBuildConfig.SetBuildTargetPlatform Start");
      if ((buildTarget >= 0 && !ResBuildConfig.SetBuildTargetPlatform((BuildTarget)buildTarget))) {
        ResBuildLog.Warn("BuildAllResources.SetBuildTargetPlatform failed!");
        return false;
      }
    
      LogBuildStatus("ResBuildLog.SetFileLog Start");
      if (!ResBuildLog.SetFileLog(true, ResBuildHelper.FormatResBuildLogFilePath())) {
        ResBuildLog.Warn("BuildAllResources.RedirectLog failed!");
        return false;
      }

      LogBuildStatus("ResBuildConfig.Check Start");
      if (!ResBuildConfig.Check()) {
        ResBuildLog.Warn("BuildAllResources.CheckConfig failed!");
        return false;
      }

      LogBuildStatus("ResDeployer.CleanCache Start");
      if (!ResDeployer.CleanCache()) {
        ResBuildLog.Warn("BuildAllResources.CleanCache failed!");
        return false;
      }

      LogBuildStatus("ResDeployer.CleanOutputResDir Start");
      if (!ResDeployer.CleanOutputResDir()) {
        ResBuildLog.Warn("BuildAllResources.CleanOutputRes failed!");
        return false;
      }

      LogBuildStatus("ResBuilder.BuildAllResources Start");
      if (!ResExporter.ExportAllResBuildData()) {
        ResBuildLog.Warn("BuildAllResources.ExportAllResBuildData failed!");
        return false;
      }

      LogBuildStatus("ResVersionGenerator.BuildResVersionFiles Start");
      if (!ResVersionGenerator.BuildResVersionFiles()) {
        ResBuildLog.Warn("BuildAllResources.BuildResVersionFiles failed!");
        return false;
      }

      //LogBuildStatus("ResSheetGenerator.BuildResSheetFile Start");
      //if (!ResSheetGenerator.BuildResSheetFile()) {
      //  ResBuildLog.Warn("BuildAllResources.BuildResSheetFile failed!");
      //  return false;
      //}

      LogBuildStatus("VersionGenerator.GenVersionFile Start");
      if (!VersionGenerator.GenVersionFile()) {
        ResBuildLog.Warn("BuildAllResources.GenVersionFile failed!");
        return false;
      }

      //LogBuildStatus("ResDeployer.BuildPackage Start");
      //if (!ResDeployer.BuildPackage()) {
      //  ResBuildLog.Warn("BuildAllResources.BuildPackage failed!");
      //  return false;
      //}

      //LogBuildStatus("ResDeployer.CommitBuildInResources Start");
      //if (!ResDeployer.CommitBuildInResources()) {
      //  ResBuildLog.Warn("BuildAllResources.CommitBuildInResources failed!");
      //  return false;
      //}

      ResBuildLog.Info("ResBuildProcesser.BuildAllResources Success");
    } catch (System.Exception ex) {
      ResBuildLog.Warn("ResBuildProcesser.BuildAllResources failed! ex:" + ex);
      ret = false;
    } finally {
      ResBuildLog.ResetLog();
    }
    return ret;
  }
  public static bool BuildClientResources(int buildTarget = -1) {
    bool ret = true;
    try {
      LogBuildStatus("ResBuildConfig.Load() Start");
      if (!ResBuildConfig.Load()) {
        ResBuildLog.Warn("BuildClientResources.LoadConfig failed!");
        return false;
      }

      LogBuildStatus("ResBuildConfig.SetBuildTargetPlatform Start");
      if ((buildTarget >= 0 && !ResBuildConfig.SetBuildTargetPlatform((BuildTarget)buildTarget))) {
        ResBuildLog.Warn("BuildClientResources.SetBuildTargetPlatform failed!");
        return false;
      }

      LogBuildStatus("ResBuildLog.SetFileLog Start");
      if (!ResBuildLog.SetFileLog(true, ResBuildHelper.FormatResBuildLogFilePath())) {
        ResBuildLog.Warn("BuildClientResources.RedirectLog failed!");
        return false;
      }

      LogBuildStatus("ResBuildConfig.Check Start");
      if (!ResBuildConfig.Check()) {
        ResBuildLog.Warn("BuildClientResources.CheckConfig failed!");
        return false;
      }

      LogBuildStatus("ResDeployer.CleanCache Start");
      if (!ResDeployer.CleanCache()) {
        ResBuildLog.Warn("BuildClientResources.CleanCache failed!");
        return false;
      }

      LogBuildStatus("ResDeployer.CleanOutputResDir Start");
      if (!ResDeployer.CleanOutputResDir()) {
        ResBuildLog.Warn("BuildClientResources.CleanOutputRes failed!");
        return false;
      }

      LogBuildStatus("ResSheetGenerator.BuildResSheetFile Start");
      if (!ResSheetGenerator.BuildResSheetFile()) {
        ResBuildLog.Warn("BuildClientResources.BuildResSheetFile failed!");
        return false;
      }

      LogBuildStatus("VersionGenerator.GenVersionFile Start");
      if (!VersionGenerator.GenVersionFile()) {
        ResBuildLog.Warn("BuildClientResources.GenVersionFile failed!");
        return false;
      }

      LogBuildStatus("ResDeployer.CommitBuildInResources Start");
      if (!ResDeployer.CommitBuildInResources()) {
        ResBuildLog.Warn("BuildClientResources.CommitBuildInResources failed!");
        return false;
      }

      ResBuildLog.Info("ResBuildProcesser.BuildClientResources Success");
    } catch (System.Exception ex) {
      ResBuildLog.Warn("ResBuildProcesser.BuildClientResources failed! ex:" + ex);
      ret = false;
    } finally {
      ResBuildLog.ResetLog();
    }
    return ret;
  }
  public static bool BuildSelectedResources(UnityEngine.Object [] selObjs, int buildTarget = -1)
  {
    bool ret = true;
    try {
      LogBuildStatus("ResBuildConfig.Load() Start");
      if (!ResBuildConfig.Load()) {
        ResBuildLog.Warn("BuildSelectedResources.LoadConfig failed!");
        return false;
      }

      LogBuildStatus("ResBuildConfig.SetBuildTargetPlatform Start");
      if ((buildTarget >= 0 && !ResBuildConfig.SetBuildTargetPlatform((BuildTarget)buildTarget))) {
        ResBuildLog.Warn("BuildSelectedResources.SetBuildTargetPlatform failed!");
        return false;
      }

      LogBuildStatus("ResBuildLog.SetFileLog Start");
      if (!ResBuildLog.SetFileLog(true, ResBuildHelper.FormatResBuildLogFilePath())) {
        ResBuildLog.Warn("BuildSelectedResources.RedirectLog failed!");
        return false;
      }

      LogBuildStatus("ResBuilder.BuildSelectedResources Start");
      if (!ResExporter.ExportSelectedResBuildData(selObjs)) {
        ResBuildLog.Warn("BuildSelectedResources.ExportSelectedResBuildData failed!");
        return false;
      }

      LogBuildStatus("ResVersionGenerator.BuildResVersionFiles Start");
      if (!ResVersionGenerator.BuildSingleResVersionFiles(true)) {
        ResBuildLog.Warn("BuildClientResources.BuildSingleResVersionFiles failed!");
        return false;
      }

      LogBuildStatus("VersionGenerator.GenVersionFile Start");
      if (!VersionGenerator.GenVersionFile()) {
        ResBuildLog.Warn("BuildClientResources.GenVersionFile failed!");
        return false;
      }

      ResBuildLog.Info("ResBuildProcesser.BuildSelectedResources Success");
    } catch (System.Exception ex) {
      ResBuildLog.Warn("ResBuildProcesser.BuildClientResources failed! ex:" + ex);
      ret = false;
    } finally {
      ResBuildLog.ResetLog();
    }
    return ret;
  }
  public static bool BuildSheetResources(int buildTarget = -1)
  {
    bool ret = true;
    try {
      LogBuildStatus("ResBuildConfig.Load() Start");
      if (!ResBuildConfig.Load()) {
        ResBuildLog.Warn("BuildSelectedResources.LoadConfig failed!");
        return false;
      }

      LogBuildStatus("ResBuildConfig.SetBuildTargetPlatform Start");
      if ((buildTarget >= 0 && !ResBuildConfig.SetBuildTargetPlatform((BuildTarget)buildTarget))) {
        ResBuildLog.Warn("BuildSelectedResources.SetBuildTargetPlatform failed!");
        return false;
      }

      LogBuildStatus("ResBuildLog.SetFileLog Start");
      if (!ResBuildLog.SetFileLog(true, ResBuildHelper.FormatResBuildLogFilePath())) {
        ResBuildLog.Warn("BuildSelectedResources.RedirectLog failed!");
        return false;
      }

      LogBuildStatus("ResBuilder.BuildSelectedResources Start");
      if (!ResExporter.ExportResSheetData()) {
        ResBuildLog.Warn("BuildSelectedResources.ExportSelectedResBuildData failed!");
        return false;
      }

      LogBuildStatus("ResVersionGenerator.BuildResVersionFiles Start");
      if (!ResVersionGenerator.BuildSingleResVersionFiles(true)) {
        ResBuildLog.Warn("BuildClientResources.BuildSingleResVersionFiles failed!");
        return false;
      }

      LogBuildStatus("VersionGenerator.GenVersionFile Start");
      if (!VersionGenerator.GenVersionFile()) {
        ResBuildLog.Warn("BuildClientResources.GenVersionFile failed!");
        return false;
      }

      ResBuildLog.Info("ResBuildProcesser.BuildSelectedResources Success");
    } catch (System.Exception ex) {
      ResBuildLog.Warn("ResBuildProcesser.BuildClientResources failed! ex:" + ex);
      ret = false;
    } finally {
      ResBuildLog.ResetLog();
    }
    return ret;
  }
  private static void LogBuildStatus(string step)
  {
    ResBuildLog.Info("*********************************************");
    ResBuildLog.Info(step);
  }
}